key = {
    _keys: [0x0F, 0x0F],
    _colidx: 0,
    rb: function () {
        //debug
        if (key._colidx == 0x10 && ((key._keys[0] >> 3) & 1) == 0) {
            enterpress = true;
            opc = 0;
        }
        //end debug
        switch (key._colidx) {
            case 0x00: return 0x00; break;
            case 0x10: return key._keys[0]; break;
            case 0x20: return key._keys[1]; break;
            default: return 0x00; break;
        }
    },
    wb: function (val) {
        key._colidx = val & 0x30;
    },
    reset: function () {
        key._keys = [0x0F, 0x0F];
        key._colidx = 0;
    },

    keydown: function (e) {
        switch (e.keyCode) {
            case 39: key._keys[1] &= 0xE; break; //right
            case 37: key._keys[1] &= 0xD; break; //left
            case 38: key._keys[1] &= 0xB; break; //up
            case 40: key._keys[1] &= 0x7; break; //down
            case 90: key._keys[0] &= 0xE; break; //A - z
            case 88: key._keys[0] &= 0xD; break; //B - x
            case 32: key._keys[0] &= 0xB; break; //Select - space
            case 13: key._keys[0] &= 0x7; break; //Start - enter
        }
    },

    keyup: function (e) {
        switch (e.keyCode) {
            case 39: key._keys[1] |= 0x1; break;
            case 37: key._keys[1] |= 0x2; break;
            case 38: key._keys[1] |= 0x4; break;
            case 40: key._keys[1] |= 0x8; break;
            case 90: key._keys[0] |= 0x1; break;
            case 88: key._keys[0] |= 0x2; break;
            case 32: key._keys[0] |= 0x5; break;
            case 13: key._keys[0] |= 0x8; break;
        }
    }
}